package cn.jdy.plugins.entity;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.mapping.Array;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;

/**
 * @desc   测评记录实体操作
 * @author ningqian
 * @date   2016-7-4
 */
@Service
public class EvaluationRecordService extends BaseDao<EvaluationRecord>{

	/**
	 * 按条件查询
	 * @param evaluationRecord
	 * @return
	 */
	public List<EvaluationRecord> query(EvaluationRecord evaluationRecord, String... date) {
		if(null == evaluationRecord) return super.findAll();
		if(evaluationRecord.getiDisplayLength() > 0){
		  return super.find(createFilter(evaluationRecord, date),evaluationRecord.getiDisplayStart(),evaluationRecord.getiDisplayLength());
		}
		return super.find(createFilter(evaluationRecord, date));
	}

	public Long count(EvaluationRecord evaluationRecord, String... date) {
		return super.count(createFilter(evaluationRecord, date));
	}
	
	/**    
	 * 查询数据导出
	 * @param evaluationRecord
	 * @param date
	 * @return
	 */
	public List<EvaluationRecord> queryExport(EvaluationRecord evaluationRecord, String... date) {
		return super.find(createFilter(evaluationRecord, date));
	}
	
	/**
	 * 拼装条件(幼儿园、兴趣班、宝贝账号、宝贝姓名、测评时间段)
	 * @param evaluationRecord
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(EvaluationRecord evaluationRecord, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	 
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("insertTime", date[0],date[1]));
	  }
	  
	  dc.addOrder(Order.desc("insertTime"));
		return dc;
	}
	
}
